<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/1/9
 * Time: 下午1:04
 */

namespace EasySwoole;

use \EasySwoole\Core\AbstractInterface\EventInterface;
use \EasySwoole\Core\Swoole\ServerManager;
use \EasySwoole\Core\Swoole\EventRegister;
use \EasySwoole\Core\Http\Request;
use \EasySwoole\Core\Http\Response;

use \EasySwoole\Core\Swoole\EventHelper;
use \App\Socket\Parser\WebSocket;


use \EasySwoole\Core\Component\Di;


use App\Socket\Logic\UserLogic;
use think\Db;


use App\Socket\Logic\DeckCards;



Class EasySwooleEvent implements EventInterface {

    public static function frameInitialize(): void
    {
        // TODO: Implement frameInitialize() method.
        date_default_timezone_set('Asia/Shanghai');
        $dbConf = Config::getInstance()->getConf("database");
        Db::setConfig($dbConf);

        Db::name("User")->where("1=1")->update(["fd"=>null,"rid"=>null]);
        Db::execute("TRUNCATE e_room;");
           
    }

    public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    {


        //注册WebSocket解析器
        EventHelper::registerDefaultOnMessage($register,WebSocket::class);
        //注册onClose事件
        $register->add($register::onClose,function(\swoole_server $server,$fd,$reactorId){            
            UserLogic::close($fd);
        });
      
       
       
        // TODO: Implement mainServerCreate() method.
    }

    public static function onRequest(Request $request,Response $response): void
    {
        // TODO: Implement onRequest() method.
    }

    public static function afterAction(Request $request,Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}